/**
 * 这是 user 模块
 */

const userInfo = localStorage.getItem('userInfo')

export default {
  // 命名空间，建议每个子模块都需要配置
  namespaced: true,

  state() {
    return {
      userInfo: userInfo ? JSON.parse(userInfo) : null
    }
  },

  getters: {
    gender(state) {
      return state.userInfo && state.userInfo.gender
    }
  },

  mutations: {
    setUserInfo(state, payload) {
      state.userInfo = payload

      localStorage.setItem('userInfo', JSON.stringify(payload))
    },

    sayHi() {
      console.log('user sayHi')
    }
  },

  actions: {
    login({ commit }, payload) {
      setTimeout(() => {
        const info = {
          name: '张三',
          age: 18,
          gender: 1
        }

        // 提交 mutation
        commit('setUserInfo', info)
      }, 1000)
    },

    sayHi() {
      console.log('user sayHi action')
    }
  }
}
